package com.santu.edu.supporter.biz.user.service.impl;

import com.santu.edu.supporter.biz.common.pojo.dto.WxLoginResponse;
import com.santu.edu.supporter.biz.common.service.WxAppReqService;
import com.santu.edu.supporter.biz.user.pojo.dto.UserDTO;
import com.santu.edu.supporter.biz.user.service.UserLoginService;
import com.santu.edu.supporter.biz.user.service.UserService;
import com.santu.edu.supporter.util.JWTUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@RequiredArgsConstructor
public class UserLoginServiceImpl implements UserLoginService {


    @Value("${wx.appId}")
    private String appId;

    @Value("${wx.appSecret}")
    private String appSecret;


    private final WxAppReqService wxAppReqService;

    private final UserService userService;

    @Override
    public String loginByCode(String code) {
        WxLoginResponse wxLoginResponse = wxAppReqService.wxLogin(code);
        UserDTO userById = userService.getUserById(wxLoginResponse.getOpenid());
        if (userById == null) {
            userService.addUser(wxLoginResponse.getOpenid());
        }
        // 调用自定义类封装的方法，创建token
        return JWTUtils.createToken(wxLoginResponse.getOpenid());
    }
}
